package com.example.petadopt.ui.adapter;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petadopt.R;
import com.example.petadopt.entity.Pet;
import com.gzone.university.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 宠物列表适配器
 */
public class PetAdapter extends RecyclerView.Adapter<PetAdapter.ViewHolder> {

    private List<Pet> petList = new ArrayList<>();
    private OnItemClickListener listener;

    public void setPetList(List<Pet> petList) {
        this.petList = petList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull android.view.ViewGroup parent, int viewType) {
        View view = android.view.LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pet_admin, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Pet pet = petList.get(position);
        holder.bind(pet, listener);
    }

    @Override
    public int getItemCount() {
        return petList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private TextView tvBreed;
        private TextView tvGender;
        private TextView tvAge;
        private Button btnEdit;
        private Button btnDelete;
        private ImageView ivPetImage;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPetImage = itemView.findViewById(R.id.iv_pet_image);
            tvName = itemView.findViewById(R.id.tv_pet_name);
            tvBreed = itemView.findViewById(R.id.tv_pet_breed);
            tvGender = itemView.findViewById(R.id.tv_pet_gender);
            tvAge = itemView.findViewById(R.id.tv_pet_age);
            btnEdit = itemView.findViewById(R.id.btn_edit);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }

        public void bind(final Pet pet, final OnItemClickListener listener) {
            tvName.setText(pet.getName());
            tvBreed.setText(pet.getBreed());
            tvGender.setText(pet.getGender());
            tvAge.setText(pet.getAge() + "岁");
            GlideUtils.load(ivPetImage, pet.getImage());

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(pet);
                    }
                }
            });

            btnEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onEditClick(pet);
                    }
                }
            });

            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onDeleteClick(pet);
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Pet pet);

        void onEditClick(Pet pet);

        void onDeleteClick(Pet pet);
    }
}
